package at.mri.dogwatch.client.pages.videodetails;

import at.mri.dogwatch.client.pages.widgets.ChromelessPlayer;
import at.mri.dogwatch.client.pages.widgets.ChromelessPlayer.PlayerListener;
import at.mri.dogwatch.client.pages.widgets.ChromelessPlayer.PlayerState;

import com.gwtplatform.mvp.client.ViewImpl;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class VideDetailsView extends ViewImpl implements
		VideDetailsPresenter.MyView, PlayerListener {

	private static final int VIDEO_HEIGHT = 350;
    private static final int VIDEO_WIDTH = 560;
	
	private final Widget widget;
	
	private static final String PLAYER_NAME = "YTPLAYER";
	
	
	@UiField ChromelessPlayer videoPlayer;
	@UiField Panel videoPanel;
	
	@UiField Label titleLabel;
	@UiField Label descriptionLabel;
	@UiField Label authorLabel;
	@UiField Label viewsLabel;
	@UiField Label lastModifiedLabel;
	

	public interface Binder extends UiBinder<Widget, VideDetailsView> {
	}

	@Inject
	public VideDetailsView(final Binder binder) {
		
		videoPlayer = new ChromelessPlayer(PLAYER_NAME);
		widget = binder.createAndBindUi(this);
		
	}
	
    @UiFactory
    ChromelessPlayer createPlayer() {
            String playerId = DOM.createUniqueId().replace("-", "");
            return new ChromelessPlayer(playerId);
    }

	@Override
	public Widget asWidget() {
		return widget;
	}
	
    @Override
    public void initializeVideo(final String videoID) {
            // THIS MUST BE SCHEDULED DFEREED!!!!
            // THE PLAYER CREATES A DIV THAT IS ATTACHED
            // AFTER THE EVENT LOOP-RETURNs
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
				
				@Override
				public void execute() {
					// TODO Auto-generated method stub
                    videoPlayer.createSwf(videoID, VIDEO_WIDTH, VIDEO_HEIGHT,
                    		VideDetailsView.this);
				}
			});
    }
    
	@Override
	public void onPlayerReady() {
		
	}

	@Override
	public void onPlayerStateChange(PlayerState state) {
		
	}
}
